#include "filetransferlogcenter.h"
#include "datacenter.h"

#include <QSqlQuery>
#include <QDateTime>
#include <QDebug>
#include <QSqlError>

FileTransferLogCenter::FileTransferLogCenter(const DataCenter &dataCetner):
		m_Database(dataCetner.getDatabase())
{
	createTable();
}

bool FileTransferLogCenter::newFileTransfer(int clientId, const QString &fileName, long long fileSize)
{
	QDateTime currentDateTime = QDateTime::currentDateTime();
	QString insertSql = "INSERT INTO FileTransferLog VALUES(%1,'%2', %3,'%4')";
	insertSql = insertSql.arg(QString::number(clientId),
							  fileName,
							  QString::number(fileSize/1024),
							  currentDateTime.toString("yyyy/MM/dd hh:mm:ss"));

	QSqlQuery sqlQuery(m_Database);
	if(!sqlQuery.exec(insertSql))
	{
		qDebug()<<m_Database.lastError();
		return false;
	}

	return true;
}

void FileTransferLogCenter::createTable()
{
	QString createSql = "CREATE TABLE FileTransferLog(ClientId int not null,"
													  "FileName varchar(max) not null,"
													  "FileSize int not null,"
													  "TransferDoneTime datetime not null,"
													  "FOREIGN KEY(ClientId) REFERENCES Client(Id) ON DELETE CASCADE)";
	QSqlQuery sqlQuery(m_Database);
	if(!sqlQuery.exec(createSql))
	{
	}
}
